package com.data.permission.rule.process;

import com.data.permission.transf.DataPermissionsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.TThreadedSelectorServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TTransportException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @包名称 com.thrift.distributed.process
 * @类描述 <pre></pre>
 * @作者 zhangbo
 * @创建时间 2019/9/19
 * @版本 1.00
 * @修改记录 <pre>
 *     版本           修改人         修改日期         修改内容描述
 *     -----------------------------------------------------------
 *     1.00           Lenovo        2019/9/19
 *     -----------------------------------------------------------
 * </pre>
 */
@Slf4j
@Component
public class ProcessStart implements Runnable {

    @Value("${PROCESS.PORT:9090}")
    private int servicePort;
    @Value("${PROCESS.SELECTOR.THREAD:20}")
    private int selectorThread;
    @Value("${PROCESS.WORKER.THREAD:100}")
    private int workerThread;
    @Autowired
    private PermissionProcess permissionProcess;

    @Override
    public void run() {
        try (TNonblockingServerSocket tNonblockingServerSocket = new TNonblockingServerSocket(servicePort)) {
            TCompactProtocol.Factory factory = new TCompactProtocol.Factory();
            DataPermissionsService.Processor<PermissionProcess> distributedServiceProcessProcessor = new DataPermissionsService.Processor<>(permissionProcess);
            TThreadedSelectorServer.Args args = new TThreadedSelectorServer.Args(tNonblockingServerSocket)
                    .transportFactory(new TFramedTransport.Factory())
                    .protocolFactory(factory)
                    .processor(distributedServiceProcessProcessor)
                    .selectorThreads(selectorThread).workerThreads(workerThread);
            TThreadedSelectorServer server = new TThreadedSelectorServer(args);
            log.info("启动数据权限服务端...");
            server.serve();
        } catch (TTransportException e) {
            log.error("服务启动失败：{}", e);
        }
    }
}
